import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import {userInfoHttp} from '@/common/http/api.js'
const store = new Vuex.Store({
    state: {
		userInfo:{},
		userId: '', //分享id
		menuButton: {},
		tree: [], //家谱树
		treeMap: new Map()
    },
    mutations: {
		setUserInfo(state,userInfo){
			state.userInfo = userInfo
		},
		setUserId(state,userId){
			state.userId = userId
		},
		setMenuButton(state,menuButton){
			state.menuButton = menuButton
		},
		setTreeItem(state,arr){
			if(Array.isArray(arr) && arr.length > 0) {
				arr.forEach(i=>{
					state.treeMap.set(i.id, i)
					this.commit('setTreeItem', i.children)
				})
			}
		},
		setTree(state,tree){
			state.treeMap.clear()
			this.commit('setTreeItem', tree)
			state.tree = tree
		}
    },
    actions: {
        async getUserInfo({ commit }){
			let token = uni.getStorageSync('token')
			if(token) {
				const res = await userInfoHttp()
				commit('setUserInfo', res.data)
				return res
			}
        	return {}
        },
		getMenuButton({ commit }) {
			// 获取胶囊按钮的位置信息
			let menuButtonRect = wx.getMenuButtonBoundingClientRect();
			console.log("Menu button rect:", menuButtonRect);
			commit('setMenuButton',menuButtonRect)
		},
		getTreeItem({state} ,id) {
			return state.treeMap.get(id)
		}
    }
})

export default store